﻿using System;

namespace IRobot
{
    public class State
    {
        public int X;
        public int Y;    // (X,Y) of a state is the cell that the robot will stand on AFTER he will take the move.
        public State Parent;
        public int heuristic;   //used only in clean BEFS
        public readonly RobotMove Move;
        public readonly int[,] RoomCells; //0=clean, 1=dirty
        public int CleanCount = 0;

        public State(int x, int y, int[,] roomCells, State parent, RobotMove move, int cleanCount)
        {
            X = x;
            Y = y;
            RoomCells = roomCells;
            Parent = parent;
            Move = move;
            CleanCount = cleanCount;
        }

        public override string ToString()
        {
            var moveIndex = (Move == null) ? "None" : Move.Index.ToString();
            return string.Format("State=({0},{1}) , Move={2}", X, Y, moveIndex);
        }

        public override bool Equals(object obj)
        {
            var other = obj as State;
            if (other == null) return false;

            bool equals = other.X == X && other.Y == Y && ArraysEqual(RoomCells, other.RoomCells);

            return equals;
        }

        static bool ArraysEqual(int[,] a1, int[,] a2)
        {
            if (a1.Length == a2.Length)
            {
                for (int i = 0; i < Math.Sqrt(a1.Length); i++)
                {
                    for (int j = 0; j < Math.Sqrt(a1.Length); j++)
                    {
                        if (a1[i, j] != a2[i, j])
                            return false;
                    }
                }
                return true;
            }
            return false;
        }
    }
}